package com.atguigu.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @Description:
 * @Author: ruyi yang
 * @date 2021/7/30 13:33
 */
@Configuration
public class CorsConfig {
    @Bean
    public CorsWebFilter  corsWebFilter(){
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        CorsConfiguration configuration = new CorsConfiguration();
        // 头
        configuration.addAllowedHeader("*");
        // 方法
        configuration.addAllowedMethod("*");
        // 源=protocol://ip:port
        // configuration.addAllowedOrigin("*");
        configuration.addAllowedOriginPattern("*");
        // 允许携带cookie跨域
        configuration.setAllowCredentials(true);
        // 任何请求都可以跨
        configurationSource.registerCorsConfiguration("/**",configuration);
        return new CorsWebFilter(configurationSource);
    }
}
